package com.zy.imagineCommunity.service;

import com.zy.imagineCommunity.dao.CommentMapper;
import com.zy.imagineCommunity.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {

    @Autowired
    CommentMapper commentMapper;

    //根据blogId、当前页码、每页个数查询评论以及评论下的回复
    public Page<CommentText> queryBlogByPraiseAndPageNoAndPageSize(int blogId, int pageNo, int pageSize) {
        Page<CommentText> page = new Page<CommentText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = commentMapper.queryCommentsCountByBlogId(blogId);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<CommentText> commentList = commentMapper.queryCommentsByBlogIdAndBeginAndPageSize(blogId, begin, pageSize);
        for (CommentText comment : commentList) {
            comment.setItems(commentMapper.queryReplyByCommentId(comment.getId()));
        }
        //设置每页内容
        page.setItems(commentList);
        return page;
    }

    //增加评论
    public void addComment(Comment comment) {
        commentMapper.addComment(comment);
    }

    //增加评论回复
    public void addReply(Reply reply) {
        commentMapper.addReply(reply);
    }

    //个人评论分页
    public Page<CommentTextTwo> queryPersonalCommentsByUserIdAndPageNoAndPageSize(int userId, int pageNo, int pageSize) {
        Page<CommentTextTwo> page = new Page<CommentTextTwo>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = commentMapper.queryPersonalCommentsCountTotalByUseId(userId);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<CommentTextTwo> blogList = commentMapper.queryPersonalCommentsByUserIdAndBeginAndPageSize(userId, begin, pageSize);
        //设置每页内容
        page.setItems(blogList);
        return page;
    }

    //通过id删除评论
    public void deleteCommentByCommentId(int commentId) {
        commentMapper.deleteCommentByCommentId(commentId);
    }

    //通过commentId删除回复
    public void deleteReplyByCommentId(int commentId) {
        commentMapper.deleteReplyByCommentId(commentId);
    }

    //查询全部评论，供超级管理员使用
    public Page<CommentTextThree> queryCommentsByPageNoAndPageSize(int pageNo, int pageSize) {
        Page<CommentTextThree> page = new Page<CommentTextThree>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = commentMapper.queryCommentsCountTotal();
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<CommentTextThree> blogList = commentMapper.queryCommentsByBeginAndPageSize(begin, pageSize);
        //设置每页内容
        page.setItems(blogList);
        return page;
    }

    //查询全部回复，供超级管理员使用
    public Page<ReplyTextTwo> queryReplyByPageNoAndPageSize(int pageNo, int pageSize) {
        Page<ReplyTextTwo> page = new Page<ReplyTextTwo>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = commentMapper.queryReplyCountTotal();
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<ReplyTextTwo> blogList = commentMapper.queryReplyTextByBeginAndPageSize(begin, pageSize);
        //设置每页内容
        page.setItems(blogList);
        return page;
    }

    public void deleteReplyById(int id) {
        commentMapper.deleteReplyById(id);
    }

    //查询板块评论，供超级版主使用
    public Page<CommentTextThree> queryCommentsByTypeAndPageNoAndPageSize(int type, int pageNo, int pageSize) {
        Page<CommentTextThree> page = new Page<CommentTextThree>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = commentMapper.queryCommentsCountTotalByType(type);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<CommentTextThree> blogList = commentMapper.queryCommentsByTypeAndBeginAndPageSize(type, begin, pageSize);
        //设置每页内容
        page.setItems(blogList);
        return page;
    }

    //查询板块回复，供版主使用
    public Page<ReplyTextTwo> queryReplyTextByTypeAndPageNoAndPageSize(int type, int pageNo, int pageSize) {
        Page<ReplyTextTwo> page = new Page<ReplyTextTwo>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = commentMapper.queryReplyCountTotalByType(type);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<ReplyTextTwo> blogList = commentMapper.queryReplyTextByTypeAndBeginAndPageSize(type, begin, pageSize);
        //设置每页内容
        page.setItems(blogList);
        return page;
    }

    /**
     * 主管理员查询评论数量
     *
     * @return
     */
    public int queryCommentsCountTotal() {
        return commentMapper.queryCommentsCountTotal();
    }

    /**
     * 主管理员查询回复数量
     *
     * @return
     */
    public int queryReplyCountTotal() {
        return commentMapper.queryReplyCountTotal();
    }

    /**
     * 用户个人评论查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<ReplyTextTwo> queryPersonalReplyByUserIdAndPageNoAndPageSize(int userId, int pageNo, int pageSize) {
        Page<ReplyTextTwo> page = new Page<ReplyTextTwo>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = commentMapper.queryPersonalReplyCountTotalByUserId(userId);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<ReplyTextTwo> blogList = commentMapper.queryPersonalReplyTextByUserIdAndBeginAndPageSize(userId, begin, pageSize);
        //设置每页内容
        page.setItems(blogList);
        return page;
    }

    /**
     * 根据版主类型查询评论数量
     *
     * @param type
     * @return
     */
    public int queryCommentsCountTotalByType(int type) {
        return commentMapper.queryCommentsCountTotalByType(type);
    }

    /**
     * 根据版主类型查询回复数量
     *
     * @param type
     * @return
     */
    public int queryReplyCountTotalByType(int type) {
        return commentMapper.queryReplyCountTotalByType(type);
    }

}
